package com.hongkuncheng.cms.controller.index;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.OrderForSql;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.DataHelper;
import com.hongkuncheng.cms.helper.StringHelper;
import com.hongkuncheng.cms.model.Article;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Transactional
@RequestMapping("search")
public class IsearchController extends BaseController {

    @RequestMapping
    public String index() {
        String searchstr = request.getParameter("searchstr") == null ? ""
                : StringHelper.escapeHtml(StringHelper.escapeSql(request.getParameter("searchstr")));
        if (!searchstr.isEmpty()) {
            Criteria criteriaArticle = factorySession.createCriteria(Article.class)
                    .add(Restrictions.eq("articleState.id", 4));
            StringBuilder callbackparams = new StringBuilder();
            Disjunction disjunction = Restrictions.disjunction();
            disjunction.add(Restrictions.like("title", searchstr, MatchMode.ANYWHERE));
            disjunction.add(Restrictions.like("digest", searchstr, MatchMode.ANYWHERE));
            disjunction.add(Restrictions.like("contenttext", searchstr, MatchMode.ANYWHERE));
            request.setAttribute("searchstr", searchstr);
            callbackparams.append("searchstr=" + searchstr);
            request.setAttribute("callbackparams", callbackparams);
            int pageIndex = request.getParameter("pageindex") == null || request.getParameter("callbackparams") != null &&
                    !callbackparams.toString().equals(request.getParameter("callbackparams")) ? 1 :
                    Integer.parseInt(request.getParameter("pageindex"));
            int datasize = 30;
            List<Article> articles = criteriaArticle.add(disjunction)
                    .addOrder(OrderForSql.asc("(case when title='" + searchstr + "' then 0 else 1 end)"))
                    .addOrder(OrderForSql.asc("(case when title like('%" + searchstr + "%') then 0 else 1 end)"))
                    .addOrder(OrderForSql.asc("(case when digest like('%" + searchstr + "%') then 0 else 1 end)"))
                    .addOrder(OrderForSql.asc("(case when contenttext like('%" + searchstr + "%') then 0 else 1 end)"))
                    .setFirstResult((pageIndex - 1) * datasize).setMaxResults(datasize).list();
            request.setAttribute("articles", articles);
            request.setAttribute("pageindex", pageIndex);
            int recordcount = articles.size() == 0 ? 0 :
                    Integer.parseInt(criteriaArticle.setProjection(Projections.countDistinct("id"))
                            .setFirstResult(0).uniqueResult().toString());
            request.setAttribute("recordcount", recordcount);
            int pagecount = DataHelper.calcPageCount(recordcount, datasize);
            request.setAttribute("pagecount", pagecount);
            frontpageCommon();
            request.setAttribute("viewcontentpath", webinfo.getTemplate() + "/search.html");
            return view();
        } else {
            print("Illegal operation");
            return null;
        }
    }

}
